<template>
  <div>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button type="primary" @click="handleCreate"> 新增</a-button>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              icon: 'clarity:note-edit-line',
              onClick: handleEdit.bind(null, record),
            },
          ]"
        />
      </template>
    </BasicTable>
    <Drawer @register="registerDrawer" @success="handleSuccess"/>
  </div>
</template>
<script lang="ts">
  import {defineComponent} from 'vue';

  import {BasicTable, useTable, TableAction} from '/@/components/Table';
  import {page, remove} from './api';

  import {useDrawer} from '/@/components/Drawer';
  import Drawer from './drawer.vue';
  import {useMessage} from "/@/hooks/web/useMessage";
  import {columns, searchFormSchema} from './data';

  export default defineComponent({
    name: 'highspeedtrainManagement',
    components: {BasicTable, Drawer, TableAction},
    setup() {
      const {createMessage} = useMessage();
      const [registerDrawer, {openDrawer}] = useDrawer();
      const [registerTable, {reload}] = useTable({
        title: '列表',
        api: page,
        columns,
        formConfig: {
          labelWidth: 120,
          schemas: searchFormSchema,
        },
        useSearchForm: true,
        showTableSetting: true,
        bordered: true,
        showIndexColumn: true,
        actionColumn: {
          width: 80,
          title: '操作',
          dataIndex: 'action',
          slots: {customRender: 'action'},
          fixed: undefined,
        },
      });

      function handleCreate() {
        openDrawer(true, {
          isUpdate: false,
        });
      }

      function handleEdit(record: Recordable) {
        openDrawer(true, {
          record,
          isUpdate: true,
        });
      }

      function handleDelete(record: Recordable) {
        console.log(record);
        const highSpeedTrainId = record.highSpeedTrainId;
        remove(highSpeedTrainId).then(() => {
          createMessage.success('删除成功');
          handleSuccess();
        }).catch((data) => {
          console.info('catch', data)
          createMessage.error('删除失败');
        })
      }

      function handleSuccess() {
        reload();
      }

      return {
        registerTable,
        registerDrawer,
        handleCreate,
        handleEdit,
        handleDelete,
        handleSuccess,
      };
    },
  });
</script>
